package wowraidmanager.client.top;

import wowraidmanager.client.BaseComposite;
import wowraidmanager.client.service.wishlist.contact.ContactService;
import wowraidmanager.client.service.wishlist.contact.ContactServiceAsync;
import wowraidmanager.client.top.image.Resources;
import wowraidmanager.client.wishlist.utils.CheckError;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Index extends BaseComposite{

    private Resources resources = GWT.create(Resources.class);

    protected ContactServiceAsync contactService = GWT.create(ContactService.class);

    private static IndexUiBinder uiBinder = GWT.create(IndexUiBinder.class);

    @UiField
    Button okButton;

    @UiField
    Button resetButton;

    @UiField
    TextBox guildName;

    @UiField
    TextBox realmName;

    @UiField
    TextBox memberName;

    @UiField
    TextBox memberAccount;


    @UiField
    Image topImage;

    @UiField
    Image editwishlistImage;

    interface IndexUiBinder extends UiBinder<Widget, Index>{
    }

    public Index(){
        initWidget(uiBinder.createAndBindUi(this));
        setImage();
        setButton();
    }

    private void setButton(){
        okButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setRegist();
            }
        });

        resetButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                clearInfo();
            }
        });
    }

    private void setImage(){
        topImage.setResource(resources.top());
        editwishlistImage.setResource(resources.editwishlist());
    }

    protected void clearInfo(){
        guildName.setText("");
        realmName.setText("");
        memberName.setText("");
        memberAccount.setText("@gmail.com");
    }

    protected void setRegist(){

        String guildNameString = guildName.getText();
        String realmNameString = realmName.getText();
        String memberNameString = memberName.getText();
        String memberAccountString = memberAccount.getText();

        errorString = CheckError.checkRegistUser(guildNameString, realmNameString, memberNameString, memberAccountString);

        if(errorString.equals("")){

            contactService.sendRegistToAdmin(guildNameString, realmNameString, memberNameString, memberAccountString, new AsyncCallback<Void>(){

                public void onSuccess(Void result){
                    setInfoMessage("メールの送信を行いました。");
                    clearErrorMessage();
                    clearInfo();
                }

                public void onFailure(Throwable caught){
                    clearInfoMessage();
                    setErrorMessage("メールの送信に失敗しました。");
                }
            });
        }else{
            clearInfoMessage();
            setErrorMessage(errorString);
        }
    }
}
